package cn.ac.iscas.action.paper;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.ac.iscas.bean.paper.ConfererncePaper;
import cn.ac.iscas.bean.paper.DocumentType;
import cn.ac.iscas.bean.paper.JournalPaper;
import cn.ac.iscas.bean.paper.PaperDocument;
import cn.ac.iscas.bean.paper.PaperType;
import cn.ac.iscas.service.paper.IConferencePaperService;
import cn.ac.iscas.service.paper.IPaperDocumentService;

@Controller @Scope("prototype")
public class ConferencePaperAddAction {
	@Resource private IConferencePaperService conferencePaperService;
	@Resource private IPaperDocumentService paperDocumentService;
	private ConfererncePaper conferencePaper;
	
	private File[] word;
	private String[] wordContentType;
	private String[] wordFileName;
	private File[] pdf;
	private String[] pdfContentType;
	private String[] pdfFileName;
	
	public void setPdfContentType(String[] pdfContentType) {
		this.pdfContentType = pdfContentType;
	}
	public String[] getPdfFileName() {
		return pdfFileName;
	}
	public void setPdfFileName(String[] pdfFileName) {
		this.pdfFileName = pdfFileName;
	}
	public String add()
	{
		return "add";
	}
	public String update() throws IOException
    {
		Set<PaperDocument> documents = conferencePaperService.get(ConfererncePaper.class, conferencePaper.getId()).getWord();
    	for(PaperDocument doc:documents)
    	{
    		doc.setPaper(conferencePaper);
    	}
    	String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<PaperDocument> paperWordDocuments = new HashSet<PaperDocument>();
		Set<PaperDocument> paperPDFDocuments = new HashSet<PaperDocument>();
		if(pdf != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<pdf.length;i++)
			{
				PaperDocument docInfo = new PaperDocument();
				String realName = UUID.randomUUID().toString()+ getExt(pdfFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(pdf[i], savefile);
				docInfo.setDocName(pdfFileName[i]);
				docInfo.setContentType(pdfContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("pdf");
				docInfo.setSize(pdf[i].length()/1024);
				docInfo.setPaper(conferencePaper);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		if(word != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<word.length;i++)
			{
				PaperDocument docInfo = new PaperDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(word[i], savefile);
				docInfo.setDocName(wordFileName[i]);
				docInfo.setContentType(wordContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("word");
				docInfo.setSize(word[i].length()/1024);
				docInfo.setPaper(conferencePaper);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		conferencePaper.setPaperType(PaperType.会议论文);
		conferencePaperService.update(conferencePaper);
		for(PaperDocument doc:documents)
    	{
			paperDocumentService.save(doc);
    	}
		for(PaperDocument doc:paperWordDocuments)
		{
			paperDocumentService.save(doc);
		}
		for(PaperDocument doc:paperPDFDocuments)
		{
			paperDocumentService.save(doc);
		}
    	return "success";
    }
    public String save() throws IOException
    {
    	String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<PaperDocument> paperWordDocuments = new HashSet<PaperDocument>();
		Set<PaperDocument> paperPDFDocuments = new HashSet<PaperDocument>();
		if(pdf != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<pdf.length;i++)
			{
				PaperDocument docInfo = new PaperDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(pdf[i], savefile);
				docInfo.setDocName(pdfFileName[i]);
				docInfo.setContentType(pdfContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("pdf");
				docInfo.setSize(pdf[i].length()/1024);
				docInfo.setPaper(conferencePaper);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		if(word != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<word.length;i++)
			{
				PaperDocument docInfo = new PaperDocument();
				String realName = UUID.randomUUID().toString()+ getExt(wordFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(word[i], savefile);
				docInfo.setDocName(wordFileName[i]);
				docInfo.setContentType(wordContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType("word");
				docInfo.setSize(word[i].length()/1024);
				docInfo.setPaper(conferencePaper);
				paperWordDocuments.add(docInfo);
			}
	   	System.out.println("上传验收文档中文档成功");
      	System.out.println(realpath);
		}
		conferencePaper.setPaperType(PaperType.会议论文);
		conferencePaperService.save(conferencePaper);
		for(PaperDocument doc:paperWordDocuments)
		{
			paperDocumentService.save(doc);
		}
		for(PaperDocument doc:paperPDFDocuments)
		{
			paperDocumentService.save(doc);
		}
    	return "success";
    }
	public IConferencePaperService getConferencePaperService() {
		return conferencePaperService;
	}
	public void setConferencePaperService(
			IConferencePaperService conferencePaperService) {
		this.conferencePaperService = conferencePaperService;
	}
	public ConfererncePaper getConferencePaper() {
		return conferencePaper;
	}
	public void setConferencePaper(ConfererncePaper conferencePaper) {
		this.conferencePaper = conferencePaper;
	}
	public void setWord(File[] word) {
		this.word = word;
	}
	public File[] getWord() {
		return word;
	}
	public void setWordContentType(String[] wordContentType) {
		this.wordContentType = wordContentType;
	}
	public String[] getWordContentType() {
		return wordContentType;
	}
	public void setWordFileName(String[] wordFileName) {
		this.wordFileName = wordFileName;
	}
	public String[] getWordFileName() {
		return wordFileName;
	}
	public static String getExt(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf("."));
	}
	public void setPaperDocumentService(IPaperDocumentService paperDocumentService) {
		this.paperDocumentService = paperDocumentService;
	}
	public IPaperDocumentService getPaperDocumentService() {
		return paperDocumentService;
	}
	public File[] getPdf() {
		return pdf;
	}
	public void setPdf(File[] pdf) {
		this.pdf = pdf;
	}
	public String[] getPdfContentType() {
		return pdfContentType;
	}
}
